// PROGRAMA : SCRPE003
// BIBLIOTECA : SISP
// SISTEMA : INFORMACOES DE SEGURANCA PUBLICA - SISP
// CLIENTE : SEJUSP - SEC. DA JUSTICA E SEGURANCA PUBLICA
// INSTITUTO DE IDENTIFICACAO
// PROGRAMADOR : * DATA: 02/08/2002
// OBJETIVO :
// OBJETIVO : EMITE QUADRO ESTATISTICO. (BATCH)
// ID. CIVIL
package br.com.vti.sisp_sicrim.antigo.controller.programa;

import java.text.ParseException;
import java.util.Arrays;

import net.sf.jasperreports.engine.JRException;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.vti.sisp_sicrim.antigo.controller.relatorios.Scrpt001Report;
import br.com.vti.sisp_sicrim.antigo.model.SicEstatistica;
import br.com.vti.sisp_sicrim.antigo.model.Sisldat8;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.Sisgsisp;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Scrpe003 OBJETIVO : OBJETIVO : EMITE QUADRO ESTATISTICO. (BATCH) ID. CIVIL *
 * 
 * @author Incode - Data: mai/2014
 * 
 */
@Resource
@Path("/antigo/scrpe003")
public class Scrpe003 {
	// Views utilizadas
	SicEstatistica vwCadest = new SicEstatistica();
	// Telas/relatorios utilizados
	Scrpt001Report report;
	// Includes utilizados
	Sisldat8 sisldat8 = new Sisldat8();
	// Areas globais / session
	Sisgsisp sisgsisp;
	// Funcoes/subrotinas
	FuncoesGerais funcoes = new FuncoesGerais();

	Result result;
	Sessao sessao;
	Boolean primeiraVez = false;
	Scrpe003 scrpe003Anterior;
	String faseproc = "0";

	// NOME DO IDENTIFICADO
	// private int[] tot0 = new int[2];
	private int[] totp = new int[2];
	private String[] conimp = new String[3];
	private int k = 0;
	private String motida = "";
	private String motid2 = "";

	public Scrpe003(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public void executar(String[] args) throws NumberFormatException, Exception {
		int ind;
		String datini = args[0];
		String datfim = args[1];
		String matric = args[2];
		String senha = args[3];
		report.setDatini(Integer.valueOf(datini));
		report.setDatfim(Integer.valueOf(datfim));
		for (ind = 1; ind <= 50; ind++) {
			report.setMmat(ind, 0);
			report.setMdata(ind, 0);
			report.setMhora(ind, 0);
			report.setMnome(ind, "");
			report.setMregger(ind, 0);
			report.setMposto(ind, 0);
			report.setMtotal(ind, 0);
		}
		Arrays.fill(totp, 0);
		Arrays.fill(conimp, "");
		Estpb131 estpb131 = new Estpb131(sessao);
		estpb131.executar(Integer.parseInt(datini), Integer.parseInt(datfim), 0, Integer.parseInt(matric), senha);
		report = new Scrpt001Report();
		// DEFINE PRINTER(BUC = 1)
		for (int i1 = 1; i1 <= 1; i1++) {
			report.setTot0(i1, 0);
		}
		motida = UtilBean.moveSubstring(motida, 0, 3, "A", "IDC");
		motida = UtilBean.moveSubstring(motida, 3, 2, "N", 99);
		// ASSIGN MAT = 34165
		// DATINI:=09092002

		motid2 = motida;
		// DATFIM:=09092002
		sisldat8.dtini = report.getDatini();
		sisldat8.dtini = (int) funcoes.sissd002(sisldat8.dtini);
		sisldat8.dtfim = report.getDatfim();
		sisldat8.dtfim = (int) funcoes.sissd002(sisldat8.dtfim);
		// MOVE DTINI TO DAT
		// MOVE DTFIM TO DA2

		while (vwCadest.leituraMotida()) {
			// READADA
			if (vwCadest.quebraDinestdatatu()) {
				// READ VW-CADEST BY DINEST-NUMPOS
				if (vwCadest.getCodigoModelo().equals("IDC") && vwCadest.getCodigoTipo() == 99 && vwCadest.getQuantTrans(7) > 0 && vwCadest.getDataAtualiz() >= sisldat8.dtini && vwCadest.getDataAtualiz() <= sisldat8.dtfim) {

					k += 1;
					if (k == 1) {

						report.setMnome(k, " ");
					} else {

						report.setMnome(k, "TOTAL..............:");
					}

					report.setMtotal(k, totp[1]);
					totp[1] = 0;
				}
			}
			if (vwCadest.getCodigoModelo().equals("IDC") && vwCadest.getCodigoTipo() == 99 && vwCadest.getQuantTrans(7) > 0 && vwCadest.getDataAtualiz() >= sisldat8.dtini && vwCadest.getDataAtualiz() <= sisldat8.dtfim) {
				motida = UtilBean.moveSubstring(motida, 5, 8, "N", vwCadest.getDataAtualiz());
				motida = UtilBean.moveSubstring(motida, 5, 8, "N", funcoes.sissd002(Integer.parseInt(UtilBean.subStr(motida, 5, 8))));
				k += 1;
				if (k > 50) {
					impr();
					k = 1;
				}

				report.setMmat(k, vwCadest.getMatriculaFunc());
				report.setMdata(k, Integer.valueOf(UtilBean.subStr(motida, 5, 8)));
				report.setMhora(k, vwCadest.getHoraem());
				report.setMnome(k, vwCadest.getNomeId());
				report.setMregger(k, vwCadest.getNumeroRg());
				report.setMposto(k, vwCadest.getNumeroPosto());
				report.setMtotal(k, (int) (report.getMtotal(k) + vwCadest.getQuantTrans(7)));
				totp[1] = totp[1] + (int) vwCadest.getQuantTrans(7);
				report.setTot0(1, (int) (report.getTot0(1) + vwCadest.getQuantTrans(7)));
			}
		}

		// END-FIND
		if (vwCadest.registrosProcessados() == 0) {
			UtilBean.mensagemTexto("INTERVALO INEXISTENTE");
		}
		impr();
		report.imprimir();
	}

	private void impr() throws ParseException, JRException {
		// RELATORIO

		report.gravaRelatorio(1);
		// report.receberTela(false, result);
	}

}
